Assignment #120 Programs That Write to Files
Code
///Name: Ian Grant
///Period:5
///Project Name: Programs That Write to Files
///File Name:Reciept.java
///05/28/16
import java.io.IOException;
import java.util.Scanner;
import java.io.PrintWriter;
public class Receipt {
public static void main(String[] args) {
Scanner stuff = new Scanner(System.in);
double price = 5;
double amount =0;
double total = 0;
System.out.println("The price is "+price+" per gallon.");
System.out.println("How many gallons? ");
amount=stuff.nextInt();
total=price*amount;
PrintWriter fileOut;
try{
fileOut = new PrintWriter("receipt.txt");
} catch(IOException e) {
System.out.println("Sorry, I can't open the file 'receipt.txt' for editing.");
System.out.println("Maybe the file exists and is read-only?");
fileOut = null;
System.exit(1);
}
fileOut.println( "+------------------------+" );
fileOut.println( "| |" );
fileOut.println( "| CORNER STORE |" );
fileOut.println( "| |" );
fileOut.println( "| 2016-01-25 04:38PM |" );
fileOut.println( "| |" );
fileOut.println( "| Gallons: "+amount+" |" );
fileOut.println( "| Price/gallon: $ "+price+".00 |" );
fileOut.println( "| |" );
fileOut.println( "| Fuel total: $ "+total+" |" );
fileOut.println( "| |" );
fileOut.println( "+------------------------+" );
fileOut.close();
}
}
Output